var TOO_LONG=-1;
var TOO_SHORT=1;
function _obj(id){
	return document.getElementById(id);
}
function checkForm(eles,frmId,fun){
	for(i=0;i<eles.length;i++){
		with(eles[i]){
			if(!validate())
				return false;
		}
	}
	if(fun)fun();
	if(frmId){
		_obj(frmId).submit();
	}
	return true;
}
function showErrorMsg(msg,htmlele){
	alert(msg);
	if(htmlele)htmlele.focus();
}

function checkLength(txt,maxlen,minlen){
	len = txt.length;
	if(maxlen>0&&maxlen<len){
		return TOO_LONG;
	}
	if(minlen>0&&minlen>len){
		return TOO_SHORT;
	}
}
function Ele(id,allownull,maxlen,minlen,descLab){
	this.id=id;
	this.allownull = allownull;
	this.maxlen = maxlen;
	this.minlen=minlen;
	this.descLab=descLab;
	this.validate=function(){
		val = _obj(this.id).value;
		if(!allownull){
			if(val==null||val==""){
				showErrorMsg('"'+this.descLab+'"不能为空',_obj(this.id));
				return false;
				}
			else
				return true;
		}
		return true;
	}
}

// email检查
function EmailEle(id, nullable, descLab) {
    this.id = id;
    this.nullable = nullable;
    this.descLab = descLab;
    this.validate = function() {
       var val = _obj(this.id).value;
       if ( !nullable ) {
		  if ( null == val || "" == val ) {
		    showErrorMsg('"'+this.descLab+'"不能为空',_obj(this.id));
		    return false;
		  } else {
			var regex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z|A-Z]{2,4}$";
			if ( !val.match(regex) ) {
			  showErrorMsg('"'+this.descLab+'"不是有效的EMail地址',_obj(this.id));
			  return false;
			}
		  }
		  return true;
		}
		return true;
    }	
}

// 日期检查
function DateEle(id, nullable, descLab) {
    this.id = id;
    this.nullable = nullable;
    this.descLab = descLab;
    this.validate = function() {
        var val = _obj(this.id).value;
        if (!nullable) {
		  if (null == val || "" == val) {
		    showErrorMsg('"'+descLab+'"不能为空',_obj(this.id));
		    return false;
		  } else {
		    var regex = "/^(\d{2,4})([|/])(\d{1,2})\2(\d{1,2})$/";
		      if (!val.match(regex)) {
		        showErrorMsg('"'+descLab+'"不是有效的日期',_obj(this.id));
		        return false;
		      }
		  }
		  return true;
		}
		return true;
    }  
}

// 数字检查
function NumberEle(id, nullable, isint, digitnum, descLab) {
    this.id = id;
    this.nullable = nullable;
    this.isint = isint;
    this.digitnum = digitnum;
    this.descLab = descLab;
    this.validate = function() {
        var val = _obj(this.id).value;
		  if (null == val || "" == val) {
		  	if(!nullable){
			    showErrorMsg('"'+descLab+'"不能为空',_obj(this.id));
			    return false;
		    }else{
		    	return true;
		    }
		  } else if(isNaN(val)) {
		    showErrorMsg('"'+descLab+'"必须是数字',_obj(this.id));
		    return false;
		  } else {
		    var regex;
		    if (isint) {
		      regex = /^[+\-]?\d+$/;
		      if (!val.match(regex)) {
		        showErrorMsg('"'+descLab+'"必须是整数',_obj(this.id));
		        return false;
		      }
		    } else {
		      regex = "^[+\-]?\\d+\.\\d{1,"+digitnum+"}$";
		        if (!val.match(regex)) {
		          showErrorMsg('"'+descLab+'"必须是'+digitnum+'位以内小数',_obj(this.id));
		          return false;
		        }
		      }
		    }
		return true;
    }  
}

function debug(msg){
	alert(msg);
}

function commit(frmId,namePairs){
	for(i=0;i<namePairs.length;i++){
		try{
		_obj(namePairs[i][0]).value=namePairs[i][1];
		}catch(ex){
			debug('未找到id:'+namePairs[i][0]);
		}
	}
	_obj(frmId).submit();
}

function clsForm(frmId){
	clrfrm=_obj(frmId);
	len=clrfrm.elements.length;
	for(i=0;i<len;i++){
		frmEle = clrfrm.elements[i];
		if(frmEle.type.toLowerCase()=='text'){
			frmEle.value='';
		}else if(frmEle.tagName.toLowerCase()=='select'){
			frmEle.selectedIndex=0;
		}
	}
	clrfrm.submit();
}
